/*
 * Ext GWT 2.2.1 - Ext for GWT
 * Copyright(c) 2007-2010, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://extjs.com/license
 */
package pe.edu.upc.dsd.jjjar.upcbank.client.mvc;

import pe.edu.upc.dsd.jjjar.upcbank.client.AppEvents;
import pe.edu.upc.dsd.jjjar.upcbank.client.BancoUPC;
import pe.edu.upc.dsd.jjjar.upcbank.client.LoginServiceAsync;
import pe.edu.upc.dsd.jjjar.upcbank.resources.client.model.Operation;
import pe.edu.upc.dsd.jjjar.upcbank.resources.client.model.UserProfile;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MenuEvent;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.event.WindowEvent;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.mvc.View;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.Viewport;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.layout.CardLayout;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.menu.Menu;
import com.extjs.gxt.ui.client.widget.menu.MenuItem;
import com.extjs.gxt.ui.client.widget.toolbar.FillToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.LabelToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.SeparatorToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootPanel;

public class AppView extends View {

	public static final String VIEWPORT = "viewport";
	public static final String MAIN_PANEL = "mainPanel";

	private final LoginServiceAsync service = (LoginServiceAsync) Registry.get(BancoUPC.LOGIN_SERVICE);
	
	private Viewport viewport;
	private ContentPanel mainPanel;

	public AppView(Controller controller) {
		super(controller);
	}

	protected void handleEvent(AppEvent event) {
		if (event.getType() == AppEvents.Init) {
			initUI();
		}
	}

	protected void initialize() {
		
		LoginDialog dialog = new LoginDialog();
		dialog.setClosable(false);
		dialog.addListener(Events.Hide, new Listener<WindowEvent>() {
			public void handleEvent(WindowEvent be) {
				Dispatcher.forwardEvent(AppEvents.Init);
			}
		});
		dialog.show();
	}

	private void initUI() {
		
		service.getCurrentProfile(new AsyncCallback<UserProfile>() {
			
			@Override
			public void onSuccess(UserProfile result) {
				
				if( result ==null ){
					
					MessageBox.alert("Sesi&oacute;n", "La sesi&oacute;n ha expirado", new Listener<MessageBoxEvent>() {
						
						@Override
						public void handleEvent(MessageBoxEvent be) {
							initialize();
						}
					});
					
					return;
				}
				
				viewport = new Viewport();
				viewport.setBorders(false);
				viewport.setLayout(new FitLayout());
				Registry.register(VIEWPORT, viewport);
				
				createContainer( result );

				RootPanel.get().add(viewport);
								
			}
			
			@Override
			public void onFailure(Throwable caught) {

			}
		});


		
	}

	private void createContainer(UserProfile user) {

		
		mainPanel = new ContentPanel();
		mainPanel.setHeaderVisible(false);
		mainPanel.setBorders(false);
		mainPanel.setBodyBorder(false);
		mainPanel.setLayout(new CardLayout());
		mainPanel.setTopComponent(createToolbar( user ));
		viewport.add(mainPanel);
		
		Registry.register(MAIN_PANEL, mainPanel);

	}

	
	private ToolBar createToolbar( UserProfile user ) {

		ToolBar toolBar = new ToolBar();

		LabelToolItem lblTitle = new LabelToolItem("Banco UPC");
		lblTitle.setStyleAttribute("font-weight", "bold");
		lblTitle.setStyleAttribute("font-size", "15px");
		lblTitle.setStyleAttribute("color", "blue");
		toolBar.add(lblTitle);
		
		Button btnMetropolitano = new Button("Metropolitano");
		Menu mnuMetropolitano = new Menu();
		MenuItem submnuMetropolitano = new MenuItem("Afiliaciones", new SelectionListener<MenuEvent>() {
			
			@Override
			public void componentSelected(MenuEvent ce) {
				BancoUPC.showPage(AppEvents.Afiliaciones);
			}
			
		});
		mnuMetropolitano.add(submnuMetropolitano);
		btnMetropolitano.setMenu(mnuMetropolitano);
		toolBar.add(btnMetropolitano);
		
		
		toolBar.add(new FillToolItem());
		
		
		toolBar.add(new LabelToolItem(user.getNames()+" "+user.getLastnames()));
		toolBar.add(new SeparatorToolItem());
		Button btnCuenta = new Button("Mi Sesi&oacute;n");
		Menu mnuCuenta = new Menu();
		MenuItem submnuCuenta = new MenuItem("Cerrar Sesi&oacute;n", new SelectionListener<MenuEvent>() {

			@Override
			public void componentSelected(MenuEvent ce) {
				service.logout(new AsyncCallback<Operation>() {

					@Override
					public void onFailure(Throwable caught) {
						MessageBox.alert("Error", "Se produjo un error con el servidor", null);
					}

					@Override
					public void onSuccess(Operation result) {
					
						Window.Location.reload();
						
					}
					
				});
			}
		});
		mnuCuenta.add(submnuCuenta);
		btnCuenta.setMenu(mnuCuenta);
		toolBar.add(btnCuenta);
		

		return toolBar;

	}
	
	

}
